<?php
namespace Home\Controller;

use Think\Controller;
use Common\Org\Wxapi\WxBaseInterfaceUtil;

// 本类由系统自动生成，仅供测试用途
class Oauth2BaseController extends Controller {
	
    public function index(){
    	if (isset ( $_GET ['code'] )) {
    		$code = $_GET['code'];
    		$rurl = $_GET['rurl'];
    		$wxBaseInterface_util = new WxBaseInterfaceUtil();
    		$wxBaseInterface_util->setCode($code);
    		
    		$get_data = $wxBaseInterface_util->getUserOpenidAndOauthAccessTokenByCode();
    		$openid = $get_data['openid'];
    		if (!isset($get_data['openid'])) {
    			echo "Network timeout, please try again!";
    			exit();
    		}

    		$user_obj = $wxBaseInterface_util->getUserInfoByOverallAccessToken($openid);
    		$oid = $user_obj['openid'];
    		if (empty($oid)) {
    			$this->error('获取用户信息出错，请重试!'.$user_obj);
    			exit();
    		}
    		
    		$where = array("openid"=>$openid);
    		$wx_user = M('wx_visitor')->where($where)->find();
    		
    		
//     		$userActionModel = M('wx_user_action');
//     		date_default_timezone_set("PRC");
//     		$times= date("Y-m-d H:i:s");
//     		$timesDay= date("Y-m-d");
//     		$timesMonth= date("Y-m");
//     		$timesYear = date("Y");
//     		//添加用户行为记录
//     		$whereUserAction = array('openid'=>$openid,'timesDay'=>$timesDay);
//     		$userAction = $userActionModel->where($whereUserAction)->find();
    		
//     		if(empty($userAction)){
    			
//     			$data = array("openid"=>$openid,"times"=>$times,"timesMonth"=>$timesMonth,"timesYear"=>$timesYear,
//     					"timesDay"=>$timesDay);
//     			$userActionModel->add($data);
//     		}

    		if (empty($wx_user)) {
    			if (!empty($oid)) {
    				date_default_timezone_set("PRC");
    				$times= date("Y-m-d H:i:s");
    				$timesDay= date("Y-m-d");
    				$timesMonth= date("Y-m");
    				$timesYear = date("Y");
    				
    				$data = array("openid"=>$user_obj['openid'],"wx_nickname"=>$user_obj['nickname'],"wx_sex"=>$user_obj['sex'],
    						"wx_photo"=>$user_obj['headimgurl'],"wx_country"=>$user_obj['country'],"wx_province"=>$user_obj['province'],
    						"wx_city"=>$user_obj['city'],'times'=>$times,'timesDay'=>$timesDay,'timesMonth'=>$timesMonth,
    						'timesYear'=>$timesYear);
    				M('wx_visitor')->add($data);
    				session('mdd_wx_user',serialize($user_obj));
    				header("Location: $rurl");
    				exit();
    			}else{
    				$user_obj['openid'] = $openid;
    				session('mdd_wx_user',serialize($user_obj));
    				header("Location: $rurl");
    				exit();
    			}
    		}else{
    			if (isset($user_obj['nickname'])) {
    				
    				$data = array("wx_nickname"=>$user_obj['nickname'],
    					"wx_sex"=>$user_obj['sex'],
    					"wx_photo"=>$user_obj['headimgurl'],
    					"wx_country"=>$user_obj['country'],
    					"wx_province"=>$user_obj['province'],
    					"wx_city"=>$user_obj['city']
    				);
	    			$where = array("id"=>$wx_user['id']);
	    			M('wx_visitor')->where($where)->save($data);
	    			
	    			session('mdd_wx_user',serialize($user_obj));
	    			header("Location: $rurl");
	    			exit();
    			}else{
    				$user_obj['openid'] = $openid;
    				session('mdd_wx_user',serialize($user_obj));
    				header("Location: $rurl");
    				exit();
    			}
    		}
    	} else {
    		echo "NO CODE";
    	}
    }
}